public class MobileAppLoginController {
    
    public String LoginUrl {get; set;}
    
    public MobileAppLoginController () 
    {
        
        
        LoginUrl = '';
        
        
        if(isSandbox())
            LoginUrl += 'https://test.salesforce.com';
        else
            LoginUrl += 'https://login.salesforce.com';
            
        LoginUrl += '/services/auth/sso/00DL00000028Xh5MAE/openidpoc?community=';
        LoginUrl += 'https://' + URL.getSalesforceBaseUrl().getAuthority() + '/mobileapp';
        
    }
    
    public PageReference forwardToCustomAuthPage()
    {
        if(URL.getSalesforceBaseUrl().getProtocol() != 'https')
        {
            String redirectUrl = 'https://' + URL.getSalesforceBaseUrl().getAuthority() + '/mobileapp';
            return new PageReference(redirectUrl);
        }
        else
            return null;
        
        
    }
    
    public String currentPod (){ 
        String server = '';
         
        if (ApexPages.currentPage() != null)
        {
            //called from VF page
            server = ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To');
        } 
        else 
        {
            //called via standard class
            server = URL.getSalesforceBaseUrl().getHost();
        }
         
        if ( server != null && server.length() > 0)
        {
            server = server.substring(0 ,server.indexOf('.')); 
        }
        return server ; 
    }
 
    public Boolean isSandbox ()
    {
        String pod = currentPod();
        
        if (pod != null && pod.length() > 0 && pod.toUpperCase().startsWith('C'))
        {
            return true;
        }
        return false;
    }
}